package com.example.ck.controller;

import com.example.ck.entity.Auditproject;
import com.example.ck.entity.Message;
import com.example.ck.service.*;
import com.example.ck.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 专家审核表(Expertaudit)表控制层
 *
 * @author makejava
 * @since 2024-03-25 12:51:32
 */
@Slf4j
@RestController
@RequestMapping("audit")
@CrossOrigin
public class AuditprojectController {
    @Autowired
    private AuditprojectService auditprojectService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private MessageService messageService;

    /**
     * 新增专家审核项目
     *
     * @param auditproject
     * @return
     */
    @PostMapping("/add")
    private ResultVo addAuditProject(@RequestParam Integer auditorNum, @RequestBody Auditproject auditproject) {
        Integer projectId1 = auditproject.getProjectId();
        projectService.updateProjectauditorNumById(projectId1, auditorNum);
        boolean b = auditprojectService.addAuditProject(auditproject);
        ResultVo rv = new ResultVo<>();
        if (b) {
            // 根据获取到的项目id来更改当前的一个状态
            Integer projectId = auditproject.getProjectId();
            projectService.updateProjectStatusById(projectId, "2");

            rv.setSuccess(true);
            rv.setCode(200);
            rv.setData(auditproject);
            rv.setMessage("待审核项目添加成功");
        } else {
            rv.setSuccess(false);
            rv.setCode(201);
            rv.setMessage("待审核项目添加失败");
        }
        return rv;
    }

    /**
     * 查询指定专家待审核项目
     */
    @GetMapping("/findByExpertName")
    private ResultVo findByExpertId(@RequestParam String expertName) {
        List<Auditproject> list = auditprojectService.getAuditProjectByUserId(expertName);
        return new ResultVo(list);
    }

    /**
     * 专家根据项目id来减少项目审核人数
     */
    @PutMapping("/update")
    private ResultVo updateProjectState(@RequestParam int id) {
        // 返回结果如果为  true -> 代表项目审核人数-1
        // 返回结果如果为 false -> 代表项目审核人数已经为0 则只需修改项目的审核状态
        boolean b = auditprojectService.setProjectStatus(id);
        ResultVo rv = new ResultVo<>();
        if (b) {
            rv.setSuccess(true);
            rv.setCode(200);
            rv.setMessage("项目已经审核成功");
        } else {
            List<Message> messageStatus = messageService.getMessageStatusByProjectId(id);
            // 获取到所有的审核状态后，判断哪一种状态（3或4）多
            int status_ty = 0;
            int status_bty = 0;
            for (Message message : messageStatus) {
                String status = message.getStatus();
                if (status.equals("3")) {
                    status_bty++;
                } else if (status.equals("4")) {
                    status_ty++;
                }
                log.info("status_ty:{}", status_ty);
                log.info("status_bty:{}", status_bty);
            }
            // 同意数量大于不同意数量
            // 则要将项目的审核状态改为通过 -> 4
            // 否则将项目的审核状态改为不通过 -> 3
            if (status_ty > status_bty) {
                projectService.updateProjectStatusById(id, "4");
            } else {
                projectService.updateProjectStatusById(id, "3");
            }
            rv.setSuccess(true);
            rv.setCode(200);
            rv.setMessage("项目已经审核完成");
        }
        return rv;
    }

}

